<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="600px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="巡检列表" hidden>
        <el-input v-model="form.checkBoxDatas" style="width: 370px;"  />
      </el-form-item>
      <!-- <el-form-item label="巡检类型" >
        <el-input v-model="form.typeName" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检对象" >
        <el-input v-model="form.objName" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="巡检类型名称" >
       <el-select v-model="form.typeId" placeholder="巡检类型" v-on:change="indexSelect($event)">
           <el-option v-for="(item,index) in inspectionType" :key="index" :label="item.name" :value="item.value">
           </el-option>
      </el-select>
      </el-form-item>

       <el-form-item label="模板名称" >
        <el-input v-model="form.templateName" style="width: 370px;"/>
      </el-form-item>
      <!-- <el-form-item label="巡检对象id" >
        <el-input v-model="form.objid" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="巡检对象" >
       <el-select v-model="form.objId" placeholder="巡检对象"  v-on:change="objectSelect($event)">
           <el-option v-for="(itemobject,index) in inspectionObject" :key="index" :label="itemobject.label" :value="itemobject.id">
           </el-option>
      </el-select>
      </el-form-item>
<!--       
      <el-form-item label="模板名称" >
        <el-input v-model="form.templateName" style="width: 370px;"/>
      </el-form-item> -->
      <!-- <el-form-item label="巡检对象id" >
        <el-input v-model="form.objId" style="width: 370px;"/>
      </el-form-item> -->
     
          <!-- <el-table v-loading="loading" :data="dataitem" size="small" style="width: 100%;"  
          :row-style="tableRowStyle" 
          :header-cell-style="tableHeaderColor">
            <el-table-column prop="index" label="序号" align="center"></el-table-column>
            <el-table-column prop="typeName" label="巡检项目"/>
            <el-table-column prop="objName" label="巡检内容"/>
            <el-table-column prop="templateName" label="合格标准"/>
            <el-table-column prop="templateDesc" label="备注"/>
          </el-table> -->
        <el-form-item label="巡检项目列表" >
                <el-table v-loading="loading" :data="dataitem" size="small" style="width: 100%;"
                :row-style="tableRowStyle"
                :header-cell-style="tableHeaderColor"
                @selection-change="changeFun"
                >
                  <el-table-column type="selection">
                  </el-table-column>
                  <el-table-column prop="index" label="序号" align="center">
                  <template slot-scope="scope">
                      {{scope.$index+1}}
                  </template>

                  </el-table-column>
                  <el-table-column prop="itemname" label="巡检项目"/>
                  <el-table-column prop="itemcontent" label="巡检内容"/>
                  <el-table-column prop="itemstandard" label="合格标准"/>
                  <el-table-column prop="remark" label="备注"/>
                  </el-table>

            </el-form-item>
  
 
      <el-form-item label="模板说明" >
        <el-input v-model="form.templateDesc" style="width: 370px;"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { add, edit} from '@/api/inspectionTemplate'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  dataitem() {
    return {
      delLoading: false, sup_this: this
    }
  },

  data() {
    debugger;
    return {
      loading: false, dialog: false,
      form: {
        //id: '',
        //typeId: '',
        typeName: '',
        checkBoxDatas:'',
        //objId: '',
        objName: '',
        templateName: '',
        templateDesc: ''
      },
      dataitem:[
      ],
      checkBoxData: [],

      dictId:0,
      itemIds:[],
      inspectionObject:[],
       inspectionType:[{
            value:1,
            name:'设备类巡检'
        },{
            value:2,
            name:'设施类巡检',
        },{
            value:3,
            name:'管理类巡检'
        }],
      rules: {
      }
    }
  },
  methods: {

    //-------------------------------------------------
    //checkPermission,
 byNameinfo(name) {
      this.byName(name).then(res => {
        this.bydictId(res.id).then(resobject => {
           this.inspectionObject=resobject;
        //根据id再进行全部查询
      })
        //根据id再进行全部查询
      })
    },
 indexSelect(event){
       debugger;
    　　//console.log(event.target.label);//在这里可以正确输出每个下拉框对应的下标值，当然输出值都是可以的
        if(event==1){
            this.byNameinfo("设备类巡检");
        }else if(event==2){
            this.byNameinfo("设施类巡检");
        }else if(event==3){
            this.byNameinfo("管理类巡检");
        }
       
        //设备类巡检对象
    },
      objectSelect(event){
        debugger;
        this.byObjectId(event).then(res => {
        this.dataitem=res;
      })
      },
            // 获取id值
    byObjectId(id) {
      return request({
        url: 'api/inspectionItem/byObjectId/' + id,
        method: 'get'
      })
    },
    // 获取id值
    byName(name) {
      return request({
        url: 'api/dict/byName/' + name,
        method: 'get'
      })
    },
        // 获取id值
    bydictId(id) {
      return request({
        url: 'api/dictDetail/findByDeptId/' + id,
        method: 'get'
      })
    },
     changeFun(val) {
       debugger;
            this.form.checkBoxDatas = val;
          //   var i=0;
          //   this.checkBoxData.forEach(item =>{

          //     console.log(item);
          //     itemIds[i]=item.id;
          //     console.log(itemIds);
          //     i++;
          // });
        },

    //------------------------------
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      debugger;
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
       // id: '',
       // typeId: '',
        typeName: '',
        itemIds:"",
        //objId: '',
        objName: '',
        templateName: '',
        templateDesc: ''
      }
    },
    // 修改table tr行的背景色
    tableRowStyle({ row, rowIndex }) {
      return 'background-color: pink'
    },
    // 修改table header的背景色
    tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background-color: lightblue;color: #fff;font-weight: 500;'
      }

    }
    
  }
}
</script>

<style scoped>

</style>
